package com.qf.android.sport.adapter.headline;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qf.android.sport.R;
import com.qf.android.sport.databinding.LayoutHeadlineDynamicImageBinding;
import com.qf.android.sport.databinding.LayoutHeadlineDynamicVideoBinding;
import com.qf.android.sport.entity.headline.Dynamic;

import java.util.List;
import java.util.Map;

/**
 * Created by wen10 on 2016/12/16.
 */

public class HeadlineDynamicRecyclerAdapter extends RecyclerView.Adapter<HeadlineDynamicRecyclerAdapter.ViewHolder> {
    private List<Dynamic.DataBean.SnapshotsBean> listSnap;
    private Map<String, Integer> variableId;

    public HeadlineDynamicRecyclerAdapter(List<Dynamic.DataBean.SnapshotsBean> listSnap, Map<String, Integer> variableId) {
        this.listSnap = listSnap;
        this.variableId = variableId;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), viewType, parent, false).getRoot();
        ViewDataBinding bind = DataBindingUtil.bind(rootView);
        ViewHolder holder = new ViewHolder(bind);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if ("".equals(listSnap.get(position).getVideo_link())) {
            LayoutHeadlineDynamicImageBinding binding = (LayoutHeadlineDynamicImageBinding) holder.binding;
            switch (listSnap.get(position).getImages().size()){
                case  4:
                    binding.headlineDynamicImageRv.setLayoutManager(new GridLayoutManager(binding.headlineDynamicImageRv.getContext(),2));
                    break;
                case 1:
                    binding.headlineDynamicImageRv.setLayoutManager(new GridLayoutManager(binding.headlineDynamicImageRv.getContext(),1));
                    break;
                default:
                    binding.headlineDynamicImageRv.setLayoutManager(new GridLayoutManager(binding.headlineDynamicImageRv.getContext(),3));
                    break;

            }
            binding.setVariable(variableId.get("image"),listSnap.get(position));
            binding.executePendingBindings();
        } else {

            LayoutHeadlineDynamicVideoBinding binding = (LayoutHeadlineDynamicVideoBinding) holder.binding;
            binding.setVariable(variableId.get("video"),listSnap.get(position));



        }
    }



    public void addAll(List<Dynamic.DataBean.SnapshotsBean> listSnap) {
        int position = listSnap.size() - 1;
        listSnap.addAll(listSnap);
        notifyItemRangeInserted(position, position + listSnap.size());
    }

    public void removeAll() {
        listSnap.clear();
        notifyItemRangeRemoved(0, listSnap.size() - 1);
    }

    @Override
    public int getItemCount() {
        return listSnap.size();
    }

    @Override
    public int getItemViewType(int position) {
        if ("".equals(listSnap.get(position).getVideo_link())) {
            return R.layout.layout_headline_dynamic_image;
        } else {

            return R.layout.layout_headline_dynamic_video;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;

        public ViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }


    }


}
